package net.longjin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import net.longjin.entity.AdviseInfo;
import net.longjin.parms.AdviseInfoParms;
import net.longjin.service.AdviseInfoService;
import net.longjin.utils.MyBatisPageUtils;
import net.longjin.enums.ResBizCode;
import net.longjin.utils.ResResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 吐槽模块
 *
 * @author 何志鹏
 * @since 2020-11-19 13:43:58
 */
@RestController
@RequestMapping("api/adviseInfo")
public class AdviseInfoController {

    /**
     * 服务对象
     */
    @Autowired
    private AdviseInfoService adviseInfoService;

    /**
     * 根据id查询详情
     *
     */
    @PostMapping("queryById")
    public ResResult selectOne(@RequestBody AdviseInfoParms adviseInfoParms){
        AdviseInfo adviseInfo = this.adviseInfoService.queryById(adviseInfoParms.getAdviseId());
        return  ResResult.success(adviseInfo);
    }

    /**
     * 添加建议
     *
     * @param adviseInfoParms
     * @return
     */
    @PostMapping("insert")
    public ResResult insert(@Validated @RequestBody AdviseInfoParms adviseInfoParms) {
        AdviseInfo adviseInfo = new AdviseInfo();
        BeanUtils.copyProperties(adviseInfoParms,adviseInfo);
        int insert = this.adviseInfoService.insert(adviseInfo);
        if(insert>0){
            return  ResResult.success();
        }
        return  ResResult.error(ResBizCode.OPERA_ERROR.getCode(),ResBizCode.OPERA_ERROR.getMsg());
    }

    /**
     * 修改建议
     *
     * @param adviseInfoParms
     * @return
     */
    @PostMapping("update")
    public ResResult update(@RequestBody AdviseInfoParms adviseInfoParms) {
        AdviseInfo adviseInfo = new AdviseInfo();
        BeanUtils.copyProperties(adviseInfoParms,adviseInfo);
        int update = this.adviseInfoService.update(adviseInfo);
        if(update>0){
            return  ResResult.success();
        }
        return  ResResult.error(ResBizCode.OPERA_ERROR.getCode(),ResBizCode.OPERA_ERROR.getMsg());
    }

    /**
     * 删除建议
     *
     * @param adviseInfoParms
     * @return
     */
    @PostMapping("deleteById")
    public ResResult deleteById(@RequestBody AdviseInfoParms adviseInfoParms) {
        int delete = this.adviseInfoService.deleteById(adviseInfoParms.getAdviseId());
        if(delete>0){
            return  ResResult.success();
        }
        return  ResResult.error(ResBizCode.OPERA_ERROR.getCode(),ResBizCode.OPERA_ERROR.getMsg());
    }


    /**
     * 吐槽列表分页查询
     *
     * @param adviseInfoParms
     * @return
     */
    @PostMapping("queryAllByLimit")
    public ResResult queryAllByLimit(@RequestBody AdviseInfoParms adviseInfoParms) {
        IPage<AdviseInfo> adviseInfoIPage = this.adviseInfoService.queryAllByLimit(adviseInfoParms);
        return  ResResult.success(adviseInfoIPage);
    }

    /**
     * 查询所有吐槽  自定义xml查询
     *
     * @param adviseInfoParms
     * @return
     */
    @PostMapping("queryAll")
    public ResResult queryAll(@RequestBody AdviseInfoParms adviseInfoParms) {
        AdviseInfo adviseInfo = new AdviseInfo();
        BeanUtils.copyProperties(adviseInfoParms,adviseInfo);
        MyBatisPageUtils.startPage(adviseInfoParms.getPageNum(), adviseInfoParms.getPageSize());
        List<AdviseInfo> adviseInfos = this.adviseInfoService.queryAll(adviseInfo);
        PageInfo<AdviseInfo> page = new PageInfo<>(adviseInfos);
        return  ResResult.success(page);
    }

}